
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人空间</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 900px;
            margin: 0 auto;
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        header {
            background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%);
            color: white;
            padding: 25px 30px;
            text-align: center;
        }

        h1 {
            font-size: 2.2rem;
            margin-bottom: 10px;
        }

        .user-info {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 30px;
            background: #f8f9fa;
            border-bottom: 1px solid #eaeaea;
        }

        .username {
            font-weight: bold;
            color: #2c3e50;
        }

        .upload-section {
            padding: 30px;
            background: #f8f9fa;
            border-bottom: 1px solid #eaeaea;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #2c3e50;
        }

        input[type="file"] {
            width: 100%;
            padding: 12px;
            border: 2px dashed #3498db;
            border-radius: 8px;
            background: white;
            cursor: pointer;
        }

        input[type="file"]:hover {
            border-color: #2980b9;
            background: #f0f8ff;
        }

        button {
            background: linear-gradient(90deg, #3498db 0%, #2980b9 100%);
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 1rem;
            font-weight: 600;
            transition: all 0.3s ease;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(52, 152, 219, 0.4);
        }

        .file-list {
            padding: 30px;
        }

        .file-list h2 {
            color: #2c3e50;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #3498db;
        }

        .file-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px;
            background: white;
            border: 1px solid #eaeaea;
            border-radius: 8px;
            margin-bottom: 12px;
            transition: all 0.3s ease;
        }

        .file-item:hover {
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            transform: translateY(-2px);
        }

        .file-name {
            font-weight: 600;
            color: #2c3e50;
            flex-grow: 1;
            margin-right: 15px;
        }

        .file-size {
            color: #7f8c8d;
            font-size: 0.9rem;
            margin-right: 15px;
        }

        .download-btn {
            background: linear-gradient(90deg, #2ecc71 0%, #27ae60 100%);
            padding: 8px 16px;
        }

        .delete-btn {
            background: linear-gradient(90deg, #e74c3c 0%, #c0392b 100%);
            padding: 8px 16px;
        }

        .message {
            padding: 15px;
            margin: 20px;
            border-radius: 6px;
            text-align: center;
            font-weight: 600;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>我的空间</h1>
        </header>
        
        <div class="user-info">
            <div class="username">当前用户: <?php echo isset($_COOKIE['login_user']) ? htmlspecialchars($_COOKIE['login_user']) : '未登录'; ?></div>
            <div class="logout">
                <button onclick="logout()">退出登录</button>
            </div>
        </div>

        <?php
        // 设置用户名
        $username = isset($_COOKIE['login_user']) ? $_COOKIE['login_user'] : '';
        
        // 检查用户是否已登录
        if (!$username) {
            echo '<div class="message error">错误：未检测到登录用户！请设置 login_user Cookie。</div>';
            exit;
        }
        
        // 存储路径
        $storage_path = './storage/' . $username;
        
        // 创建用户目录（如果不存在）
        if (!is_dir($storage_path)) {
            mkdir($storage_path, 0777, true);
        }
        
        // 处理文件上传
        $message = '';
        $message_type = '';
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
            $file = $_FILES['file'];
            if ($file['error'] === UPLOAD_ERR_OK) {
                $target_path = $storage_path . '/' . basename($file['name']);
                if (move_uploaded_file($file['tmp_name'], $target_path)) {
                    $message = '文件上传成功！';
                    $message_type = 'success';
                } else {
                    $message = '文件上传失败！';
                    $message_type = 'error';
                }
            } else {
                $message = '文件上传出错！错误代码: ' . $file['error'];
                $message_type = 'error';
            }
        }
        
        // 处理文件删除
        if (isset($_GET['delete'])) {
            $file_to_delete = urldecode($_GET['delete']);
            $file_path = $storage_path . '/' . $file_to_delete;
            if (file_exists($file_path)) {
                unlink($file_path);
		header("Location: index.php");
            }
        }
        
        // 显示消息
        if ($message) {
            echo '<div class="message ' . $message_type . '">' . htmlspecialchars($message) . '</div>';
        }
        ?>

        <div class="upload-section">
            <h2>📤 上传文件</h2>
            <form method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="file">选择文件:</label>
                    <input type="file" name="file" id="file" required>
                </div>
                <button type="submit">上传文件</button>
            </form>
        </div>

        <div class="file-list">
            <h2>📋 我的文件</h2>
            <?php
            // 获取存储目录中的文件列表
            $files = array_diff(scandir($storage_path), array('..', '.'));
            
            if (empty($files)) {
                echo '<p style="text-align:center; color:#7f8c8d; font-style:italic;">暂无文件</p>';
            } else {
                foreach ($files as $file) {
                    $file_path = $storage_path . '/' . $file;
                    $file_size = filesize($file_path);
                    $file_size_formatted = formatFileSize($file_size);
                    
                    echo '<div class="file-item">';
                    echo '<span class="file-name">' . htmlspecialchars($file) . '</span>';
                    echo '<span class="file-size">' . $file_size_formatted . '</span>';
                    echo '<a href="' . $file_path . '" download="' . urlencode($file) . '"><button class="download-btn">下载</button></a>';
                    echo '<a href="?delete=' . urlencode($file) . '"><button class="delete-btn">删除</button></a>';
                    echo '</div>';
                }
            }
            
            // 格式化文件大小
            function formatFileSize($size) {
                $units = array('B', 'KB', 'MB', 'GB');
                $unit_index = 0;
                while ($size >= 1024 && $unit_index < count($units) - 1) {
                    $size /= 1024;
                    $unit_index++;
                }
                return round($size, 2) . ' ' . $units[$unit_index];
            }
            ?>
        </div>
    </div>

    <script>
        function logout() {
            // 清除登录Cookie
            document.cookie = "login_user=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
            alert("已退出登录");
            location.reload();
        }
    </script>
</body>
</html>
